package com.lablzh.gdxy.controller;

import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.admin.*;
import com.lablzh.gdxy.service.ParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/parameters")
public class ParametersController {
    @Autowired
    private ParametersService parametersService;
    @Autowired
    private UserDetailsUtil userDetailsUtil;

    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/resTeacherCourseParameters")
    public Map getResTeacherCourseParameters(@RequestParam("page") Integer page) {
        String username = userDetailsUtil.getUserDetails();
        return parametersService.getCourseParameterByUsername(username, page);
    }

    /**
     * 修改缺省参数
     *
     * @param topicNumber
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PutMapping("/topicNumber")
    public String updateTopicNumber(@RequestBody TopicNumber topicNumber) {
        return parametersService.updateTopicNumber(topicNumber);
    }

    /**
     * 获取缺省参数
     *
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/topicNumber")
    public TopicNumber getTopicNumber() {
        return parametersService.getTopicNumber();
    }

    /**
     * 添加互选参数
     *
     * @param parameter
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PostMapping("/parameter")
    public String addParameter(@RequestBody Parameter parameter) {
        System.out.println(parameter.toString());
        return parametersService.addParameter(parameter);
    }

    /**
     * 修改互选参数
     *
     * @param parameter
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PutMapping("/parameter")
    public String updateParameter(@RequestBody Parameter parameter) {
        System.out.println("=======================");
        System.out.println(parameter.toString());
        System.out.println("=======================");
        return parametersService.updateParameter(parameter);
    }

    /**
     * 删除互选参数
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @DeleteMapping("/parameter")
    public String deleteParameter(@RequestParam("ID") Integer id) {
        return parametersService.deleteParameter(id);
    }

    /**
     * 获取所有互选参数
     *
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/parameter")
    public List<Parameter> getAllParameter() {
        return parametersService.getAllParameter();
    }

    /**
     * 根据college  college type获取参数
     *
     * @param college
     * @param college
     * @param type
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/oneParameter")
    public Parameter getOneParameter(@RequestParam("college") String college,
                                     @RequestParam("grade") String grade,
                                     @RequestParam("type") String type) {
        return parametersService.getOneParameter(college, grade, type);
    }

    /**
     * 增加栏目
     *
     * @param column
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PostMapping("/column")
    public String addColumn(@RequestBody Columns column) {
        return parametersService.addColumn(column);
    }

    /**
     * 获取所有栏目
     *
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher' )")
    @GetMapping("/column")
    public List<Columns> getAllColumn() {
        return parametersService.getAllColumn();
    }

    /**
     * 根据id获取栏目
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/oneColumn")
    public Columns getOneColumn(@RequestParam("ID") Integer id) {
        return parametersService.getOneColumn(id);
    }

    /**
     * 修改栏目
     *
     * @param column
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PutMapping("/column")
    public String updateColumn(@RequestBody Columns column) {
        return parametersService.updateColumn(column);
    }

    /**
     * 删除栏目
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @DeleteMapping("/column")
    public String deleteColumn(@RequestParam("ID") Integer id) {
        return parametersService.deleteColumn(id);
    }

    @PreAuthorize("hasAnyRole('admin')")
    @GetMapping("/columndelete")
    public String deleteColumn1(@RequestParam("ID") Integer id) {
        return parametersService.deleteColumn(id);
    }

    /**
     * 添加类别
     *
     * @param category
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @PostMapping("/category")
    public String addCategory(@RequestBody Category category) {
        return parametersService.addCategory(category);
    }

    /**
     * 获取所有栏目
     *
     * @return
     */

    @GetMapping("/category")
    public List<Category> getAllCategory() {
        return parametersService.getAllCategory();
    }

    /**
     * 根据id获取类别
     *
     * @param id
     * @return
     */

    @GetMapping("/oneCategory")
    public Category getOneCategory(@RequestParam("ID") Integer id) {
        return parametersService.getOneCategory(id);
    }

    /**
     * 修改类别
     *
     * @param category
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PutMapping("/category")
    public String updateCategory(@RequestBody Category category) {
        return parametersService.updateCategory(category);
    }

    /**
     * 删除类别
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @DeleteMapping("/category")
    public String deleteCategory(@RequestParam("ID") Integer id) {
        return parametersService.deleteCategory(id);
    }

    /**
     * 根据栏目获取类别
     *
     * @param columnName
     * @return
     */
    @GetMapping("/categoryByColumn")
    public List<Category> getCategoryByColumn(@RequestParam("columnName") String columnName) {
        return parametersService.getCategoryByColumn(columnName);
    }

    /**
     * 添加专题
     * @param subject
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PostMapping("/subject")
    public String addSubject(@RequestBody Subject subject) {
        return parametersService.addSubject(subject);
    }

    /**
     * 获取所有专题
     *
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/subject")
    public List<Subject> getAllSubject() {
        return parametersService.getAllSubject();
    }

    /**
     * 根据id获取专题
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @GetMapping("/oneSubject")
    public Subject getOneSubject(@RequestParam("ID") Integer id) {
        return parametersService.getOneSubject(id);
    }

    /**
     * 修改专题
     *
     * @param subject
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @PutMapping("/subject")
    public String updateSubject(@RequestBody Subject subject) {
        return parametersService.updateSubject(subject);
    }

    /**
     * 删除专题
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin','teacher')")
    @DeleteMapping("/subject")
    public String deleteSubject(@RequestParam("ID") Integer id) {
        return parametersService.deleteSubject(id);
    }


    @PreAuthorize("hasRole('teacher')")
    @GetMapping("/teacherParameters")
    public Map getTeacherParameters(@RequestParam("grade") String grade,
                                    @RequestParam("courseCategory") String courseCategory,
                                    @RequestParam("college") String college) {
        return parametersService.getTeacherParameter(grade, courseCategory, college);
    }



}
